<?php
require('../../lib/base.php');
$base=new base(array('web','advertiser'));
$api=$base->vars('app','encryption');
$security=$base->get('security');
$ip=$base->server('REMOTE_ADDR');
if(md5($api.$ip)===$security){
	$config=$base->model('config');
	$init=$base->vars('app','init');$aptitude=$base->vars('app','aptitude');
	/*********************初始化广告投放数据***************************/
	$APC=array();
	$server=reset($config->GConfig(array('where'=>array('label='=>'advertiser','name='=>'server'))));
	$APC['server']=$server['value'];
	$material=reset($config->GConfig(array('where'=>array('label='=>'advertiser','name='=>'material'))));
	$APC['material']=$material['value'];
	$ad=$base->model('advertisers');$sort=$base->model('sort');
	$advertisers=$ad->GAd(array('select'=>'id,link,material,format,modality,tactic','where'=>array('online='=>1)));
	$label=$sort->GSort(array('where'=>array('label IN'=>array('format','modality'),'locked='=>0)));
	foreach($advertisers as $value){
		$value['tactic']=unserialize($value['tactic']);
		if(!empty($value['tactic']['china'])){
			foreach($value['tactic']['china'] as $k=>$v){
				$value['tactic']['china'][$k]=$aptitude['china'].':'.$v;
			}
			$value['tactic']['china']=implode(',',$value['tactic']['china']);
		}
		if(!empty($value['tactic']['adcolumn'])){
			foreach($value['tactic']['adcolumn'] as $k=>$v){
				$value['tactic']['adcolumn'][$k]=$aptitude['adcolumn'].':'.$v;
			}
			$value['tactic']['adcolumn']=implode(',',$value['tactic']['adcolumn']);
		}
		if(!empty($value['tactic']['keyword'])){
			$value['tactic']['keyword']=$aptitude['keyword'].':'.$value['tactic']['keyword'];
		}
		$value['tactic']=empty($value['tactic'])?'':implode(',',$value['tactic']);
		$modality=$label[$value['modality']];
		$script=array('func'=>$label[$modality['parent']]['value'],'format'=>$label[$value['format']]['value'],'size'=>$modality['value'],'material'=>$value['material']);
		$ad=array('id'=>$value['id'],'link'=>$value['link'],'format'=>$value['format'],'tactic'=>$value['tactic'],'script'=>$script);
		$APC[$value['format'].':'.$value['modality']][]=$ad;
		$APC['0:'.$value['modality']][]=$ad;
	}
	$label=$sort->GSort(array('where'=>array('label IN'=>array('browser','system','language'),'locked='=>0)));$user=array();
	foreach($label as $value){
		$user[$value['value']]=$value['id'];
	}
	$ad=fopen('ic.d','w');
	fwrite($ad,serialize(array('ad'=>$APC,'label'=>$user)));fclose($ad);
	echo 'SUCCESS';
	/*********************初始化数据统计所需服务设置***************************/
}else{
	echo 'ERROR';
}
?>